//app.js
App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    // 登录
   this.getMid(function(){})
   /**
    * 获取用户信息
    */
      wx.getUserInfo({
        withCredentials:true,
        success: res => {
          console.log(res)
          // 可以将 res 发送给后台解码出 unionId
            this.globalData.userInfo = res.userInfo
            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
            // 所以此处加入 callback 以防止这种情况
            if (this.userInfoReadyCallback) {
              console.log("ew")
              this.userInfoReadyCallback(res)
            }  
        },
        // 未授权的情况下，昵称，性别，头像为空
        fail:()=>{
          var userInfo = { nickName: "", gender: "", avatarUrl:"" }
          this.globalData.userInfo=userInfo;
          console.log(userInfo.nickName)
          if (this.userInfoReadyCallback) {
            console.log(userInfo)
            this.userInfoReadyCallback(userInfo)
          }
        }
      })
  },
  /**
   * 获取openID
   */
  getOpenId:function(callBack){
      wx.request({
        url: this.globalData.yuming + 'merchant.php/Api/Login/getOpenid',
        data:{
          js_code: this.globalData.js_code
        },
        header:{
          "content-type":"application/json"
        },
        method:"GET",
        success:function(res){
          callBack(res)
        },
        fail:function(res){
         console.log(res.data.message)
        }
      })
  },
 /**
  * 获取m_id，存放在user里面，user.m_id
  */
  getMid:function(callBack){
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        var that = this;
        var js_code = res.code;
        that.globalData.js_code = js_code;
        // 获取openid
        that.getOpenId(function (res) {
          that.globalData.openId = res.data.openid;
          if (that.globalData.userInfo) {
            var userInfo = that.globalData.userInfo;
            // console.log(userInfo)
            var openid = that.globalData.openId;
            if (openid && openid != "") {
              wx.request({
                url: that.globalData.yuming + 'merchant.php/Api/Login/login',
                data: {
                  openid: that.globalData.openId,
                  nickname: userInfo.nickName,
                  headurl: userInfo.avatarUrl,
                  gender: userInfo.gender
                },
                header: {
                  "content-type": "application/x-www-form-urlencoded"
                },
                method: "POST",
                success: function (res) {
                  var user = res.data.data;
                  that.globalData.user = user;
                  callBack();
                }
              })
            } else {
              wx.showModal({
                title: '提示',
                content: '登录失败',
                showCancel: false
              })
            }
          } else {
            that.userInfoReadyCallback = res => {
              wx.request({
                url: that.globalData.yuming + 'merchant.php/Api/Login/login',
                data: {
                  openid: that.globalData.openId,
                  nickname: res.nickName,
                  headurl: res.avatarUrl,
                  gender: res.gender
                },
                header: {
                  "content-type": "application/x-www-form-urlencoded"
                },
                method: "POST",
                success: function (res) {
                  var user = res.data.data;
                  that.globalData.user = user;
                  callBack();
                }
              })
            }
          }

        })
      }
    })
  },
  /**
   * 版本更新
   */
  upData:function(){
    var updata = wx.getUpdateManager();
    //检查新版本
    updata.onCheckForUpdate();
    // 下载
    updata.onUpdateReady(function(){
      wx.showModal({
        title: '提示',
        content: '是否使用最新版本',
        success:function(res){
          if(res.confirm){
            // 安装新版本
            updata.applyUpdate()
          }
        }
      })
    })
  },
  globalData: {
    userInfo: null,
    js_code:"",
    yuming:"https://shop.qufutong.me/",
    openId:"",
    // 跳转商品详情页的商品信息
    productModel:{},
    // shop_id为自己定的（上线后获取）
    shop_id:"1",
    // 用户的m_id和昵称，头像 
    user:{},
    // 分享时，使用的图片
    headPic:"",
    // 分享时，使用的title
    name:"商城"
  }
})